package controllers;

import play.*;
import play.mvc.*;
import play.data.validation.*;

import java.util.*;

import org.joda.time.DateMidnight;
import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;

import models.*;

public class Komponents extends Application {
    
    @Before
    static void checkUser() {
        if(connected() == null) {
            flash.error("Please log in first");
            Application.index();
        }
    }
    
    public static void index() {
        List<Booking> bookings = Booking.find("byUser", connected()).fetch();
        render(bookings);
    }

    public static void list(String search, Integer size, Integer page) {
        List<Komponent> komponents = null;
        page = page != null ? page : 1;
        if(search.trim().length() == 0 ) { 
            komponents = Komponent.all().fetch(page, size);
        } else {
            search = search.toLowerCase();
            komponents = Komponent.find("lower(komp_name) like ?", "%"+search+"%").fetch(page, size);
        }  
        render(komponents, search, size, page);
    }
    
    public static void show(Long id) {
        Komponent komponent = Komponent.findById(id);
        render(komponent);
    }
    
    public static void Current_Bookings() {
    	List<Booking> bookings = Booking.find("byUser", connected()).fetch();
    	render("@current_bookings", bookings);
    }
    
    public static void book(Long id) {
        Komponent komponent = Komponent.findById(id);
        render(komponent);
    }
    
    public static void confirmBooking(Long id, Booking booking) {
        Komponent komponent = Komponent.findById(id);
        booking.komponent = komponent;
        booking.user = connected();
        validation.valid(booking);
        validation.future(booking.checkoutDate, booking.checkinDate);
        
        // Errors or revise
        if(validation.hasErrors() || params.get("revise") != null) {
        	flash.error("Please make sure the 'Check out date' is later than the 'check in date'");
            render("@Show", komponent, booking);
        }
        
        // Confirm
        if(params.get("confirm") != null) {
            booking.create();
            flash.success("Thank you, %s, your confimation number for %s is %s", connected().name, komponent.komp_name, booking.id);
            index();
        }
        
        // Display booking
        render(komponent, booking);
    }
    
    
    
    public static void cancelBooking(Long id) {
        Booking booking = Booking.findById(id);
        booking.delete();
        flash.success("Booking cancelled for confirmation number %s", booking.id);
        index();
    }
    
    public static void settings() {
        render();
    }
    
    public static void saveSettings(String password, String verifyPassword) {
        User connected = connected();
        //connected.password = password;
        //validation.valid(connected);
        //validation.required(verifyPassword);
        //validation.equals(verifyPassword, password).message("Your password doesn't match");
        //if(validation.hasErrors()) {
        //    render("@settings", connected, verifyPassword);
        //}
        connected.save();
        //flash.success("Password updated");
        index();
    }
    
}

